# deploy jar path
SERVER_NAME=christmas-biz
SERVER_FOLDER=${SERVER_NAME}
SERVER_JAR_PATH="riptide-app/app-christmas/${SERVER_NAME}/target/${SERVER_NAME}.jar"

# deploy marchine host list
DEPLOY_USER=root
DEPLOY_PORT=22
ACCESS_TOKEN=e9526eb327a1c1d731518db1593c623bcd240ee3fd4f0dee0feb63d0dabd6fe6

# remote marchine config
REMOTE_DEPLOY_PATH="/data/deploy/riptide-app"
JAVA_HOME=/home/jenkins/jdk/jdk1.8.0_241/bin/java
JVM_OPS="-Xms32m -Xmx64m -Xmn32m -XX:MaxMetaspaceSize=128m"
PROFILE=dev
RUN_OPS="--spring.profiles.active=${PROFILE}"
BUILD_USER=`echo ${BUILD_USER} | sed "s/ /_/g"`

for DEPLOY_HOST in ${DEPLOY_HOSTS[@]}; do
	# after mvn build success.
	function sendDing() {
    curl -i -X POST -H "Content-Type: application/json;charset=utf-8" -i --data '{
      "msgtype": "actionCard",
      "actionCard": {
        "title": "自动部署-'${SERVER_NAME}'",
        "text": "### 【'${SERVER_NAME}'】'${PROCESS}'\n- 后端项目:\t'${SERVER_NAME}'\n- 服务器:\t'${DEPLOY_HOST}'\n- 环境:\t'${PROFILE}'\n- 操作人:\t'${BUILD_USER}'",
        "hideAvatar": "0",
        "btnOrientation": "0"
      },
      "at": {
        "atMobiles": [

        ],
        "isAtAll": false
      }
    }' https://oapi.dingtalk.com/robot/send?access_token=${ACCESS_TOKEN}
  }

	# check jar is exist.
	if [ ! -f "${SERVER_JAR_PATH}" ]; then
	  echo "${SERVER_JAR_PATH} is not exist. please mvn build before."
	  exit -1
	fi
	# 1. mkdir remote folder
	ssh -p ${DEPLOY_PORT} ${DEPLOY_USER}@${DEPLOY_HOST} "mkdir -p ${REMOTE_DEPLOY_PATH}/${SERVER_NAME}"
	ssh -p ${DEPLOY_PORT} ${DEPLOY_USER}@${DEPLOY_HOST} "mkdir -p ${REMOTE_DEPLOY_PATH}/${SERVER_NAME}/logs"

	# 2. offline loadbalance

	# kill ${ALIVE_PID}
	ALIVE_PID=`ssh -p ${DEPLOY_PORT} ${DEPLOY_USER}@${DEPLOY_HOST} ps -ef | grep -E ${SERVER_NAME}.jar | grep -v grep | awk '{print $2}' | awk -F '/' '{print $NF}' | sed 's/\|.jar\|.jar//g'`
	while [[ ${ALIVE_PID} != '' ]]; do
		echo "[WARNING] killing Service Named = ${SERVER_NAME} PID = ${ALIVE_PID}"
		kill -9 ${ALIVE_PID}
		sleep 2
		echo "[WARNING] -------------------sleep 2 seconds waiting process down-------"
		ALIVE_PID=`ssh -p ${DEPLOY_PORT} ${DEPLOY_USER}@${DEPLOY_HOST} ps -ef | grep -E ${SERVER_NAME}.jar | grep -v grep | awk '{print $2}' | awk -F '/' '{print $NF}' | sed 's/\|.jar\|.jar//g'`
	done

	# 4. scp local jar to remote marchine
	scp -P ${DEPLOY_PORT} -r ${SERVER_JAR_PATH} ${DEPLOY_USER}@${DEPLOY_HOST}:${REMOTE_DEPLOY_PATH}/${SERVER_NAME}

	# 5. start up Tips: append JVM_OPS
	ssh -p ${DEPLOY_PORT} ${DEPLOY_USER}@${DEPLOY_HOST} "\
		nohup ${JAVA_HOME} ${JVM_OPS} -jar ${REMOTE_DEPLOY_PATH}/${SERVER_NAME}/${SERVER_NAME}.jar \
		${RUN_OPS} >> ${REMOTE_DEPLOY_PATH}/${SERVER_NAME}/logs/${SERVER_NAME}-start.log 2>&1 & \
	"

	# 6. when deploy success. online loadbalacne
	curl -i -X POST -H "Content-Type: application/json;charset=utf-8" -i --data '{
        "msgtype": "actionCard",
            "actionCard": {
            "title": "自动部署-'${SERVER_NAME}'",
            "text": "### 【'${SERVER_NAME}'】部署成功\n- 项目:\t'${SERVER_NAME}'\n- 服务器:\t'${DEPLOY_HOST}'\n- 环境:\t'${PROFILE}'\n- 操作人:\t'${BUILD_USER}'",
            "hideAvatar": "0",
            "btnOrientation": "0"
        },
        "at": {
            "atMobiles": [
              
            ],
        "isAtAll": false
        }
    }' https://oapi.dingtalk.com/robot/send?access_token=${ACCESS_TOKEN}

done